package com.partvoyage.client.mvp2.view.infopoint;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.ListBox;
import com.partvoyage.client.mvp2.event.LoadDataInformationGeneralEvent;
import com.partvoyage.client.mvp2.event.LoadDataInformationGeneralEvent.TYPE_INFORMATION_GENERAL;
import com.partvoyage.client.mvp2.event.LoadDataInformationPointEvent;
import com.partvoyage.client.mvp2.event.LoadDataInformationPointEvent.TYPE_INFORMATION_POINT;
import com.partvoyage.client.mvp2.framework.MVPDisplay;
import com.partvoyage.client.mvp2.framework.MVPPresenter;
import com.partvoyage.client.service.calendrier.CalendarService;
import com.partvoyage.client.service.calendrier.CalendarServiceAsync;
import com.partvoyage.client.service.infopoint.InfoPointService;
import com.partvoyage.client.service.infopoint.InfoPointServiceAsync;
import com.partvoyage.client.sessionClient.SessionClient;
import com.partvoyage.shared.Constants;
import com.partvoyage.shared.InfoPointBean;
import com.smartgwt.client.data.RecordList;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.HasClickHandlers;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.HLayout;

public class InformationPointPresenter extends
		MVPPresenter<InformationPointPresenter.Display> {

	// private static final LinkedHashMap<String, String> commentMap;
	// static {
	// commentMap = new LinkedHashMap<String, String>();
	// commentMap.put(COMMENT_A_PIED, "A pied");
	// commentMap.put(COMMENT_VELO, "A vélo");
	// commentMap.put(COMMENT_VOITURE, "En voiture");
	// commentMap.put(COMMENT_AVION, "En avion");
	// }

	private static List<String> commentList;
	static {
		commentList = new ArrayList<String>();
		commentList.add(Constants.COMMENT_A_PIED);
		// commentList.add(COMMENT_VELO);
		commentList.add(Constants.COMMENT_VOITURE);
		// commentList.add(COMMENT_AVION);
	}

	protected final InfoPointServiceAsync infoService = GWT
			.create(InfoPointService.class);
	private final CalendarServiceAsync calendarService = GWT
			.create(CalendarService.class);

	public interface Display extends MVPDisplay {
		HasClickHandlers getValidationCommentaire();

		HasClickHandlers getValidationQuand();

		DynamicForm getFormCommentaire();

		// DynamicForm getFormComment();

		DynamicForm getFormQuand();

		IButton getSynchroniserAvecCalendrier();

		IButton getValidationAvecQui();

		ListGrid getAmisGrid();

		void miseAJourCommentaire(String commentaire);

		void miseAJourCout(String cout);

		DynamicForm getFormCout();

		IButton getValidateCout();

		void miseAJourQuand(Date quand);

		ListBox getCommentSelectItem();

		IButton getValidateComment();

		Integer getCommentSelectionne();

		// void initialiseComment(LinkedHashMap<String, String> commentMap);
		void initialiseComment(List<String> list);

		// void miseAJourOu(String ou);
	}

	public InformationPointPresenter(final Display display,
			final HandlerManager eventBus) {
		super(display, eventBus);
		bind();
		initialise();
	}

	private void initialise() {
		display.initialiseComment(commentList);
		// display.getFormComment().getField("comment").setValueMap(commentMap);

	}

	/**
	 * Lie le bouton send à l'action ....
	 */
	protected void bind() {
		// display.getButton().addClickHandler(new ClickHandler() {
		//
		// @Override
		// public void onClick(ClickEvent event) {
		// eventBus.fireEvent(new MettreAJourAutreCaseEvent("tut"));
		//
		// }
		// });
		display.getValidationCommentaire().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				InfoPointBean infoBean = SessionClient.getInstance()
						.getInfoPointBeanCourant();
				infoBean.setCommentaire((String) display.getFormCommentaire()
						.getField("commentaire").getValue());
				infoService.update(infoBean,
						new AsyncCallback<InfoPointBean>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub

							}

							@Override
							public void onSuccess(InfoPointBean result) {
								// TODO Auto-generated method stub

							}

						});
			}
		});
		display.getValidateCout().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				InfoPointBean infoBean = SessionClient.getInstance()
						.getInfoPointBeanCourant();
				infoBean.setCout((String) display.getFormCout()
						.getField("cout").getValue());
				infoService.update(infoBean,
						new AsyncCallback<InfoPointBean>() {

							@Override
							public void onFailure(Throwable caught) {
								System.out.println("Echec " + caught);

							}

							@Override
							public void onSuccess(InfoPointBean result) {
								eventBus.fireEvent(new LoadDataInformationGeneralEvent(
										TYPE_INFORMATION_GENERAL.TYPE_COUT));

							}

						});
			}
		});
		display.getValidateComment().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				InfoPointBean infoBean = SessionClient.getInstance()
						.getInfoPointBeanCourant();
				Integer itemComment = display.getCommentSelectionne();
				infoBean.setComment(String.valueOf(itemComment));
				infoService.update(infoBean,
						new AsyncCallback<InfoPointBean>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub

							}

							@Override
							public void onSuccess(InfoPointBean result) {
								// TODO Auto-generated method stub

							}

						});
			}
		});
		display.getValidationQuand().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				InfoPointBean infoBean = SessionClient.getInstance()
						.getInfoPointBeanCourant();
				Date quand = (Date) display.getFormQuand()
						.getField("dateTimeItem").getValue();
				eventBus.fireEvent(new LoadDataInformationGeneralEvent(
						TYPE_INFORMATION_GENERAL.TYPE_QUAND, infoBean));
				infoBean.setQuand(quand);
				infoService.update(infoBean,
						new AsyncCallback<InfoPointBean>() {

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub

							}

							@Override
							public void onSuccess(InfoPointBean result) {
								System.out
										.println("[client] Date mise à jour: "
												+ result.getQuand());
							}

						});
			}
		});
		display.getSynchroniserAvecCalendrier().addClickHandler(
				new ClickHandler() {
					public void onClick(ClickEvent event) {
						calendarService.ajouterEvenement("essai titre ",
								"essai content", new AsyncCallback() {

									@Override
									public void onFailure(Throwable caught) {
										// TODO Auto-generated method stub
										System.out.println("echec");
									}

									@Override
									public void onSuccess(Object result) {
										// TODO Auto-generated method stub
										System.out.println("success");
									}
								});
					}
				});
		display.getValidationAvecQui().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				RecordList dataAsRecordList = display.getAmisGrid()
						.getDataAsRecordList();
				System.out.println();
			}
		});
		// validateCout.addClickHandler(new ClickHandler() {
		// public void onClick(ClickEvent event) {
		// partenaireVoyageEvent.ajouterCoutPourPointCourant((String) form
		// .getField("cout").getValue());
		// }
		// });
		// validateComment.addClickHandler(new ClickHandler() {
		// public void onClick(ClickEvent event) {
		// formComment.validate(false);
		// }
		// });

	}

	@Override
	public void go(final HasWidgets container) {

	}

	public void go(HLayout container) {
		// container.clear();
		container.addChild(display.asWidget());

	}

	@Override
	protected void registerHandler() {
		// gestion des données des informations du point courant
		eventBus.addHandler(
				LoadDataInformationPointEvent.TYPE,
				new LoadDataInformationPointEvent.LoadDataInformationPointEventHandler() {

					@Override
					public void onLoadDataInformationPoint(
							final LoadDataInformationPointEvent event) {
						if (event.getTypeInformation().equals(
								TYPE_INFORMATION_POINT.TYPE_NETTOYAGE)) {
							// display.miseAJourOu(null);
							display.miseAJourCommentaire(null);
							display.miseAJourCout(null);
							display.miseAJourQuand(null);
						} else {
							InfoPointBean infoPointBean = event
									.getInfoPointBean();
							// display.miseAJourOu(infoPointBean.getOu());
							display.miseAJourCommentaire(infoPointBean
									.getCommentaire());
							String comment = infoPointBean.getComment();
							if (comment != null && !comment.isEmpty()) {
								display.getCommentSelectItem().setItemSelected(
										Integer.valueOf(comment), true);
							} else {
								display.getCommentSelectItem().setItemSelected(
										0, true);
							}
							display.miseAJourCout(infoPointBean.getCout());
							display.miseAJourQuand(infoPointBean.getQuand());
						}
					}

				});

	}
}
